﻿using Laboratory.Entities.User;
using AutoMapper;
using System.Collections.Generic;
using System.Linq;

namespace Laboratory.Users.Dto
{
    public static class Map
    {

        public static RoleDto ToDto(this Role model)
        {
            Mapper.Initialize(x => x.CreateMap<Role, RoleDto>());
            RoleDto dto = Mapper.Map<RoleDto>(model);
            return dto;
        }

        public static AccountDto ToDto(this Account model)
        {
            Mapper.Initialize(x => x.CreateMap<Account, AccountDto>());
            AccountDto dto = Mapper.Map<AccountDto>(model);
            return dto;
        }




        public static MenuDto ToDto(this Menu model)
        {
            Mapper.Initialize(x => x.CreateMap<Menu, MenuDto>());
            MenuDto dto = Mapper.Map<MenuDto>(model);
            return dto;
        }

        public static List<MenuItemDto> ToDtos(this List<Menu> models)
        {
            List<Menu> models0 = models.Where(x => x.ParentId == null).ToList();

            List<MenuItemDto> dots0 = models0.Select(x => x.ToItemDto()).ToList();

            foreach (MenuItemDto item in dots0)
            {
                List<Menu> models1 = models.Where(x => x.ParentId == item.Id).ToList();
                item.Menus = models1.ToChildItemDtos();
            }

            return dots0;
        }

        private static List<MenuItemDto> ToChildItemDtos(this List<Menu> models)
        {
            List<MenuItemDto> dots0 = models.Select(x => x.ToItemDto()).ToList();

            foreach (MenuItemDto item in dots0)
            {
                List<Menu> models1 = models.Where(x => x.ParentId == item.Id).ToList();
                item.Menus = models1.ToChildItemDtos();
            }
            return dots0;
        }

        private static MenuItemDto ToItemDto(this Menu model)
        {
            Mapper.Initialize(x => x.CreateMap<Menu, MenuItemDto>());
            MenuItemDto dto = Mapper.Map<MenuItemDto>(model);
            return dto;
        }

    }
}
